package com.rc.saas.mini.common.mq;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 日志及业务消息发送器
 */
@Component
public class MqProducer {
    private static Logger logger = LogManager.getLogger(MqProducer.class);

    @Autowired
    private AmqpTemplate amqpTemplate;

    /**
     * 直接发送
     * @param queueKey
     * @param object
     */
    public void send(String queueKey, Object object) {
        try {
            if (object != null) {
                amqpTemplate.convertAndSend(queueKey, object);
            }

            //amqpTemplate.convertAndSend(queueKey, object, new MessagePostProcessor() {
            //    @Override
            //    public Message postProcessMessage(Message message) throws AmqpException {
            //        message.getMessageProperties().setContentType("text/plain");
            //        //message.getMessageProperties().setContentEncoding("UTF-8");
            //        message.getMessageProperties().setContentEncoding("utf-8");
            //        return message;
            //    }
            //});
        } catch (Exception e) {
            logger.error("send 报错", e);
        }
    }

    /**
     * 指定推送次数,延迟时间1秒
     * @param queueKey
     * @param object
     * @param notifyCount 发送计数器
     */
    public void send(String queueKey, Object object, Integer notifyCount) {
        try {
            if (object != null) {
                amqpTemplate.convertAndSend(queueKey, object, new MessagePostProcessor() {

                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        //用于设置延迟时间，单位：ms
                        message.getMessageProperties().setDelay(1000);
                        message.getMessageProperties().setHeader("notifyCount", notifyCount);
                        //System.out.println(sdf.format(new Date()) + " Delay sent.");
                        return message;
                    }
                });
            }
        } catch (Exception e) {
            logger.error("send 报错", e);
        }
    }

    /**
     * 指定推送次数和延迟时间(秒)
     * @param queueKey
     * @param object
     * @param notifyCount
     * @param delay
     */
    public void send(String queueKey, Object object, Integer notifyCount, Integer delay) {
        try {
            if (object != null) {
                amqpTemplate.convertAndSend(queueKey, object, new MessagePostProcessor() {

                    @Override
                    public Message postProcessMessage(Message message) throws AmqpException {
                        message.getMessageProperties().setDelay(delay);
                        message.getMessageProperties().setHeader("notifyCount", notifyCount);

                        //System.out.println(sdf.format(new Date()) + " Delay sent.");
                        return message;
                    }
                });
            }
        } catch (Exception e) {
            logger.error("send 报错", e);
        }
    }

    /**
     * 重复通知间隔时间（单位为秒）
     * @param notifyCount 已经通知的次数
     * @return
     */
    private int getSpacingInterval(int notifyCount) {
        int spacingInterval = 3;
        switch (notifyCount) {
            case 1:
                spacingInterval = 10;
                break;
            case 2:
                spacingInterval = 20;
                break;
            case 3:
                spacingInterval = 30;
                break;
            case 4:
                spacingInterval = 60;
                break;
            case 5:
                spacingInterval = 90;
                break;
            default:
                break;
        }

        return spacingInterval;
    }
}
